/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actor.weapons;

import Actor.Actor;
import Actor.BulletHitMarker;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.List;
import javax.vecmath.Point2d;

/**
 *
 * @author Eric-Standard
 */
public class TargetedMissileLauncher extends actor.weapons.Weapon {

    private Actor target;
    private boolean tracking = false;
    private boolean closeTrack = false;
    private int trackingTime = 0;
    private final int trackingTimeToLock = 2000;
    private final int lockedTime = 2000;
    private final int PLAYER_TRACK_DIST = 100;
    private final int PLAYER_TRACK_DIST_SQ = PLAYER_TRACK_DIST * PLAYER_TRACK_DIST;
    private boolean lockedOn = false;
    public static final int TRACK_RECT_OFFSET = 5;

    public TargetedMissileLauncher(Actor owner) {
        super(owner);
        this.ammo = 1;
        this.clipSize = 1;
        this.totalAmmo = 5;
        this.maxAmmo = 5;
    }

    @Override
    public void act(long dt) {
        super.act(dt);
        if (tracking && trackingTime <= trackingTimeToLock) {
            if (!closeTrack) {
                trackingTime -= dt;
                lockedOn = false;
                if (trackingTime <= 0) {
                    trackingTime = 0;
                    tracking = false;
                    target = null;
                }
            } else if (!lockedOn) {
                trackingTime += dt;
                if (trackingTime >= trackingTimeToLock) {
                    trackingTime = trackingTimeToLock;
                    lockedOn = true;
                }
            }
        }
        if (tracking && target == null) {
            tracking = false;
            lockedOn = false;
            target = null;
        }
        if (tracking && target != null) {
            if (!target.isActive) {
                tracking = false;
                lockedOn = false;
                target = null;
            }
        }
        if (owner == world.getPlayer()) {
            lockByPlayer();
        }
    }

    @Override
    public void draw(Graphics g) {
        if (tracking) {
            Rectangle2D rect = target.getBoundingRectangle();
            if (rect != null) {
                Rectangle bounds = rect.getBounds();
                bounds.x -= TRACK_RECT_OFFSET;
                bounds.y -= TRACK_RECT_OFFSET;
                bounds.width += 2 * TRACK_RECT_OFFSET;
                bounds.height += 2 * TRACK_RECT_OFFSET;
                g.setColor(Color.RED);
                if (lockedOn) {
                    g.drawRect(bounds.x, bounds.y,
                            bounds.width, bounds.height);
                }
                //Top-left
                g.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + 10);
                g.drawLine(bounds.x, bounds.y, bounds.x + 10, bounds.y);
                //Top-right
                g.drawLine(bounds.x + bounds.width, bounds.y, bounds.x + bounds.width, bounds.y + 10);
                g.drawLine(bounds.x + bounds.width - 10, bounds.y, bounds.x + bounds.width, bounds.y);
                //Bottom-left
                g.drawLine(bounds.x, bounds.y + bounds.height, bounds.x + 10, bounds.y + bounds.height);
                g.drawLine(bounds.x, bounds.y + bounds.height - 10, bounds.x, bounds.y + bounds.height);
                //Bottom-right
                g.drawLine(bounds.x + bounds.width, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y + bounds.height - 10);
                g.drawLine(bounds.x + bounds.width, bounds.y + bounds.height, bounds.x + bounds.width - 10, bounds.y + bounds.height);

            }
        }
    }

    @Override
    public boolean fire() {
        if (!lockedOn) {
            return false;
        }
        if (!super.fire()) {
            if (ammo == 0) {
                reload();
            }
            return false;
        }
        if (lockedOn) {
            TargetedMissile r = new TargetedMissile(owner, target);
            world.add(r);
        }
        return true;
    }

    public void lockByPlayer() {
        Point point = world.mouseMonitor.getPoint();
        Point2d p = world.getPointFromMousePoint(point);
        List<Actor> actors = world.getActors();
        double minDistance = 10000;
        Actor nextTarget = null;
        for (Actor a : actors) {
            if (a == owner) {
                continue;
            } else if (a instanceof BulletHitMarker) {
                continue;
            }
            double dist = a.getPos().distanceSquared(p);
            if (dist < minDistance) {
                minDistance = dist;
                nextTarget = a;
            }
        }
        if (nextTarget == target) {
            closeTrack = true;
        } else if (nextTarget != null) {
            if (target == null || minDistance < target.getPos().distanceSquared(p) - 10) {
                target = nextTarget;
                tracking = true;
                closeTrack = true;
                lockedOn = false;
                trackingTime = 0;
            }
        } else if (lockedOn) {
            lockedOn = false;
            closeTrack = false;
            trackingTime = trackingTimeToLock;
        }
    }

    public void lock(Actor a) {
        target = a;
    }
}
